package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.BusinessDto;
import com.qk.dto.BusinessPoolDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Clue;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.BusinessTrackRecordMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.BusinessService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;

    @Autowired
    private ClueMapper clueMapper;


    @Override
    public PageResult<Business> listBusiness(BusinessDto businessDto) {
        Page<Business> page = businessMapper.listBusiness(new Page<Business>(businessDto.getPage(), businessDto.getPageSize()), businessDto);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    @Override
    public void assignBusiness(Integer businessId, Integer userId) {
        Business business = new Business();
        business.setId(businessId);
        business.setUserId(userId);
        business.setStatus(2); //待跟进
        business.setUpdateTime(LocalDateTime.now());
        this.updateById(business);
    }

    @Override
    public Business getBusinessById(Integer id) {
        return businessMapper.getBusinessById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void trackBuisness(Business business) {
        //1. 更新商机的信息
        business.setUpdateTime(LocalDateTime.now());
        business.setStatus(3);
        businessMapper.updateById(business);

        //2. 添加跟进记录
        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        businessTrackRecord.setBusinessId(business.getId());
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        businessTrackRecord.setTrackStatus(business.getTrackStatus());
        businessTrackRecord.setKeyItems(business.getKeyItems().toString());
        businessTrackRecord.setNextTime(business.getNextTime());
        businessTrackRecord.setRecord(business.getRecord());
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecordMapper.insert(businessTrackRecord);
    }

    @Override
    public void toBusiness(Integer id) {
        //1. 根据ID更新线索状态 (转为商机)
        Clue clue = clueMapper.selectById(id);

        clue.setStatus(5);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);

        //2. 保存商机信息 (根据 线索 组装 商机信息)
        Business business = new Business();
        BeanUtils.copyProperties(clue, business);
        business.setId(null); //  设置商机ID为空, 表示为新增商机, 主键自动增长
        business.setUserId(null); //  设置商机归属人为空
        business.setNextTime(null); //  设置下次联系时间为空
        business.setStatus(1);
        business.setClueId(clue.getId());
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());

        businessMapper.insert( business);
    }

    @Override
    public PageResult<Business> getBusinessesPoolByPage(BusinessPoolDto businessPoolDto) {
        Page<Business> page = businessMapper.getPoolBusinesses(new Page<Business>(businessPoolDto.getPage(), businessPoolDto.getPageSize()), businessPoolDto);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }
}
